package com.yiren.manager.ctrl.desk;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrVolunteerOrder;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.desk.YrVolunteerOrderForm;
import com.yiren.manager.service.iface.desk.YrVolunteerOrderService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("volunteerOrder")
@Controller
public class YrVolunteerOrderController extends GenericController<YrVolunteerOrderForm, YrVolunteerOrder>{

	@Autowired
	private YrVolunteerOrderService selfService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrVolunteerOrderForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrVolunteerOrderForm> result = new HTTPResult<YrVolunteerOrderForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrVolunteerOrderForm form = tool.mapper("volunteerOrder", YrVolunteerOrderForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getVolunteerOrderId()||form.getVolunteerOrderId().isEmpty()){
			form.setVolunteerOrderId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrVolunteerOrder entity = ObjectUtils.objectToObject(form, YrVolunteerOrder.class);
		selfService.save(entity);
		
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrVolunteerOrderForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrVolunteerOrderForm> result = new HTTPResult<YrVolunteerOrderForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			selfService.delete(id);
		}
		return result;
	}	
	
	@RequestMapping("findByMonth")
	@ResponseBody
	public HTTPResult<YrVolunteerOrderForm> findByMonth(String month) throws Exception{
		HTTPResult<YrVolunteerOrderForm> result = new HTTPResult<YrVolunteerOrderForm>();
		String sqlWhere = "DATE_FORMAT(create_time,'%Y-%m') = ?1";
		QueryResult<YrVolunteerOrder> qr = selfService.find(YrVolunteerOrder.class, sqlWhere, new Object[]{month});
		
		List<YrVolunteerOrderForm> formList = new ArrayList<YrVolunteerOrderForm>();
		for(YrVolunteerOrder entity : qr.getResultList()){
			formList.add(toForm(entity));
		}
		result.setData(formList);
		
		return result;
	}

	@Override
	public YrVolunteerOrderForm toForm(YrVolunteerOrder e) throws Exception {
		YrVolunteerOrderForm form = ObjectUtils.objectToObject(e, YrVolunteerOrderForm.class);
		return form;
	}

	@Override
	public YrVolunteerOrder toEntity(YrVolunteerOrderForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
